//
//  SLTipAlertView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/23.
//

#import "SLTipAlertView.h"

@implementation SLTipAlertView
-(void)awakeFromNib{
    [super awakeFromNib];
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    _doneBtn.layer.cornerRadius = 25;
    _doneBtn.layer.masksToBounds = YES;
    _doneBtn.btnStyle = 1;
}

+(SLTipAlertView *)showWith:(NSString *)title content1:(NSString *)content1 content2:(NSString *)content2 doneBtn:(NSString *)doneBtn cancelBtn:(NSString *)cancelBtn action:(void(^)(NSInteger index))action{
    
    __weak SLTipAlertView *alert = [SLTipAlertView viewFromXIB];
    alert.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    [kAppWindow addSubview:alert];
    alert.titleLbl.text = @"";
    alert.contentLbl.text = @"";
    alert.contentLbl2.text = @"";
    //配置
    CGFloat height = 20;
    if (kSTRING(title).length > 0) {
        CGSize titleSize = STRING_SIZE_FONT(kScreenWidth-120, title, kFontMedium(22));
        height += titleSize.height+20;
        alert.titleLbl.text = title;
    }
    if (kSTRING(content1).length > 0) {
        CGSize contentSize1 = STRING_SIZE_FONT(kScreenWidth-80, content1, kFont(16));
        height += contentSize1.height+20;
        alert.contentLbl.text = content1;
    }else{
        
    }
    
    if (kSTRING(content2).length > 0) {
        CGSize contentSize2 = STRING_SIZE_FONT(kScreenWidth-80, content2, kFont(14));
        height += contentSize2.height+20;
        alert.contentLbl2.text = content2;
    }
    height += 20;
    if (kSTRING(doneBtn).length > 0) {
        [alert.doneBtn setTitle:doneBtn forState:UIControlStateNormal];
        height += 50;
    }
    if (kSTRING(cancelBtn).length > 0) {
        [alert.cancelBtn setTitle:cancelBtn forState:UIControlStateNormal];
        height += 50+10;
    }else{
        alert.cancelBtnHeight.constant = 0;
        alert.cancelBtn.hidden = YES;
    }
    height += 25;
    alert.bgViewHeight.constant = height;
    kWeakSelf(alert);
    [alert setBtnBlock:^(NSInteger btnIndex) {
        [weakalert removeFromSuperview];
        if (action) action(btnIndex);
    }];
    return alert;
}

- (IBAction)btnAction:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(0);
    }
}

- (IBAction)doneAction:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(1);
    }
}

@end
